package com.tianyiyan.dao.tyenword;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.tianyiyan.hibernate.tyenword.Book;
import com.tianyiyan.utils.HibernateUtils;
public class BookDao
{
	public List<Book> getBookList()
	{
		List<Book> books = new ArrayList<Book>();
		Session session = HibernateUtils.getInstance().getSession();
		Transaction transaction = session.beginTransaction();
		try
		{
			String hql = "FROM Book";
			Query query = session.createQuery(hql);
			books = query.list();
			transaction.commit();
		}
		catch(Exception ex)
		{
			if (null != transaction)
			{
				transaction.rollback();
				ex.printStackTrace();
			}
		}
		return books;
	}
	
	public Book getBook(Integer id)
	{
		Session session = HibernateUtils.getInstance().getSession();
		Transaction transaction = session.beginTransaction();
		Book book = null;
		try
		{
			book = (Book)session.get(Book.class, id);
			transaction.commit();
		}
		catch(Exception ex)
		{
			if (null != transaction)
			{
				transaction.rollback();
				ex.printStackTrace();
			}
		}
		return book;
	}
}
